#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//ship structure
struct ship
{
  int id;
  int contentsk[1000];
  char *contents[1000];
};
//function prototypes
void join_convoy(int kContainers);

void leave_convoy(int shipId);

// global variables.
struct ship convoy[10000];
int noOfShips=0;
int kContainers=0;
int main(){
  int x;// Event number
  int sid;//ship id
  int position;//Ship position in convoy
  const int shippos;//Ship position 
  const int contpos;//Container position
  while (1){
    printf("\nInput:");
    scanf("%i",&x);
    switch(x){
    case 0:
      //remove ship from convoy
      //printf("\nEnter the id of the ship to remove from convoy:  ");
      //printf("");
      scanf("%d",&sid);//read the ship id from the user
      leave_convoy(sid);//leave the convoy
      main();//reload the main program ,start all over again
    case 1:
      //join convoy
      //printf("\nEnter the number of containers in the ship: ");
      scanf("%d",&kContainers);//read the n containers
      join_convoy(kContainers);// join the convoy
      main();// reload the program
    case 2:
      //get position of ship in convoy and return its id
      //printf("\nEnter the position of the ship: ");
      scanf("%d",&position);//read ship position
      printf("Output: %d",convoy[position-1].id);//print the id of the ship
      main();
    case 3:
      //get position of ship and position of container in ship and return its contents
      //printf("\nEnter position of the ship in convoy: ");
      scanf("%d",&shippos);
      //printf("\nEnter the container position: ");
      scanf("%d",&contpos);
      // if the number of ships is less than the given ship position in the convoy
      if (noOfShips<shippos || contpos>100){
	printf("\nOutput: -1");
      }
      else{
	printf("\nOutput: %s",convoy[shippos-1].contents[contpos-1]);
      }
      main();
    case -1:
      printf("Goodbye have a beautiful time.\n");
      exit(0);
    default:
      exit(0);
    }
  }
  return 0;
}

void join_convoy(int kContainers){
  struct ship newShip;
  newShip.id=noOfShips+1;
  for(int i=0;i<kContainers;i++){
    int k;
    
    //printf(":\n");
    scanf("%d",&k);
    char content[k];
    scanf("%s",&content);
    //printf("Enter the contents(k characters): ");
    //printf("\n");
    newShip.contentsk[i]=k;
    newShip.contents[i]=&content[i];
  }
  convoy[noOfShips]=newShip;
  noOfShips+=1;
  //newShip.id
  //get number of containers
  //get number of chars of contents
  //get the characters contents
}

void leave_convoy(int shipId){
  //search for ship id in convoy
  for(int i=0;i<noOfShips;i++){
    if (convoy[i].id==shipId && i!=noOfShips-1){
      struct ship tempship=convoy[i];
      convoy[i]=convoy[noOfShips-1];
      noOfShips-=1;
    }
    else if (i==noOfShips-1){
      noOfShips-=1;
    }
    else{
      printf("\nThe Ship Is not in the convoy!\n");
      exit(0);
    }
  }
}
